﻿using Newtonsoft.Json;
using SmartPlant.WinCore.Helper;
using System;

namespace SmartPlant.WinCore.Config
{
    /// <summary>
    /// 配置文件助手类。
    /// </summary>
    public static class ConfigHelper
    {
        /// <summary>
        /// 读取配置文件。
        /// </summary>
        /// <returns></returns>
        public static SysConfig? ReadConfig()
        {
            string fileName = GetConfigFileName();
            string content = FileHelper.ReadFileText(fileName);
            var config = JsonConvert.DeserializeObject<SysConfig>(content);
            return config;
        }

        /// <summary>
        /// 写入配置文件。
        /// </summary>
        /// <param name="sysConfig"></param>
        public static void WriteConfig(SysConfig sysConfig)
        {
            string content = JsonConvert.SerializeObject(sysConfig);
            string fileName = GetConfigFileName();
            FileHelper.WriteFileText(fileName, content);
        }
        /// <summary>
        /// 获取配置文件的路径+文件名
        /// </summary>
        /// <returns></returns>
        private static string GetConfigFileName()
        {
            string fileName = AppDomain.CurrentDomain.BaseDirectory.CombinePath("\\Config\\SysConfig.json");
            return fileName;
        }
    }
}
